﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoGame.Helper;
using SoGame.Model;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;
using SoGame.Views;

namespace SoGame.ViewModels
{
    public class ReconnaissanceViewModel : BaseViewModel
    {
        private bool _CanDisplayKinectSource;
        private bool _userIsReconize;

        public System.Windows.Media.Imaging.BitmapSource KinectSource
        {
            get
            {
                return KinectHelper.Instance.ImageSource;
            }
        }

        public System.Windows.Visibility IsKinectReady
        {
            get
            {
                return KinectHelper.Instance.IsKinectStarted ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            }
        }

        public System.Windows.Visibility CanDisplayKinectSource
        {
            get
            {
                return _CanDisplayKinectSource ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            }
        }

        public bool UserIsReconize
        {
            get { return _userIsReconize; }
            set
            {
                _userIsReconize = value;
                this.OnPropertyChanged(() => this.UserIsReconize);
            }
        }

        public Player CurrentPlayer
        {
            get
            {
                UserIsReconize = KinectHelper.Instance.CurrentPlayer != null ? true : false;
                return KinectHelper.Instance.CurrentPlayer;
            }
        }

        public ObservableCollection<Player> Players
        {
            get
            {
                return KinectHelper.Instance.Players;
            }
        }

        public ICommand ShowGamePlay { get { return new BaseCommand(ShowGamePlayPopup); } }

        private void ShowGamePlayPopup()
        {
            if(CurrentPlayer != null && CurrentPlayer.GamePlays != null)
                (new GamePlaysPopup() { DataContext = CurrentPlayer.GamePlays}).ShowDialog();
        }

        public ReconnaissanceViewModel()
        {
            StartKinect();
        }

        private void StartKinect()
        {
            if (KinectHelper.Instance.IsKinectStarted)
            {
                KinectHelper.Instance.StartRecognition();
                _CanDisplayKinectSource = true;

                OnPropertyChanged(() => CanDisplayKinectSource);

                KinectHelper.Instance.PropertyChanged += (ks, ke) =>
                {
                    if (ke.PropertyName == "ImageSource")
                    {
                        OnPropertyChanged(() => KinectSource);
                    }
                };
                KinectHelper.Instance.PropertyChanged += (ks, ke) =>
                {
                    if (ke.PropertyName == "CurrentPlayer")
                    {
                        OnPropertyChanged(() => CurrentPlayer);
                    }
                };
                KinectHelper.Instance.PropertyChanged += (ks, ke) =>
                {
                    if (ke.PropertyName == "Players")
                    {
                        OnPropertyChanged(() => Players);
                    }
                };
            }
        }

        private void StopKinect()
        {
            if (KinectHelper.Instance.IsKinectStarted)
            {
                KinectHelper.Instance.PauseKinect();
            }

            _CanDisplayKinectSource = false;

            OnPropertyChanged(() => CanDisplayKinectSource);
        }

        public void KinectUp()
        {
            KinectHelper.Instance.KinectCameraUp();
        }

        public void KinectDown()
        {
            KinectHelper.Instance.KinectCameraDown();
        }
    }
}
